package xuegao.study.mall.admin.controller.exam;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.admin.feign.AskServiceClient;
import xuegao.study.mall.common.api.CommonResult;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/api/v1/ask")
public class AskExamController {

    private final Logger LOGGER = LoggerFactory.getLogger(ExamController.class);

    @Autowired
    private AskServiceClient askServiceClient;

    @ApiOperation("试卷列表")
    @PostMapping(value = "/getAll")
    public CommonResult<JSONObject> getAll(@RequestBody JSONObject data) {
        data.put("Source", "1");
        String body = askServiceClient.getAskExamsList(data);
        LOGGER.info("试卷列表查询|data:{}, body:{}", data, body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        return CommonResult.success(jsonObject);
    }

    @ApiOperation("开始考试-试卷详情")
    @PostMapping(value = "/detail/{id}")
    public CommonResult<JSONObject> detail(@PathVariable("id") String id) {
        String detail = askServiceClient.getAskExamsDetail(id);
        JSONObject jsonObject = JSONObject.parseObject(detail);
        return CommonResult.success(jsonObject);
    }

    @ApiOperation("试卷详情-题目")
    @PostMapping(value = "/questions/{examId}")
    public CommonResult<JSONArray> questions(@PathVariable("examId") String examId) {
        String body = askServiceClient.getExamsQuestionList(examId);
        JSONArray arrItems = JSONArray.parseArray(body);
        return CommonResult.success(arrItems);
    }

    @ApiOperation("考试记录-创建")
    @PostMapping(value = "/record/create")
    public CommonResult<JSONObject> createExamRecord(@RequestBody JSONObject requestBody) {
        LOGGER.info("params:{}", requestBody);
        String body = askServiceClient.createExamRecord(requestBody);
        JSONObject res = JSONObject.parseObject(body);
        LOGGER.info("创建考试记录|data:{}, body:{}", requestBody, body);
        return CommonResult.success(res);
    }

    @ApiOperation("考试记录-提交")
    @PostMapping(value = "/record/submit")
    public CommonResult<JSONObject> submitExamRecord(@RequestBody JSONObject requestBody) {
        LOGGER.info("params:{}", requestBody);
        String body = askServiceClient.submitExamRecord(requestBody);
        JSONObject res = JSONObject.parseObject(body);
        LOGGER.info("提交考试记录|data:{}, body:{}", requestBody, body);
        return CommonResult.success(res);
    }

    @ApiOperation("考试记录-查询")
    @PostMapping(value = "/record/detail/{recordId}")
    public CommonResult<ExamRecords> getExamRecord(@PathVariable("recordId") String recordId) {
        LOGGER.info("recordId:{}", recordId);
        String body = askServiceClient.getExamRecord(recordId);
        JSONObject jsonObject = JSONObject.parseObject(body);
        ExamRecords records = new ExamRecords();
        records.setCorrectCount(15);
        records.setWrongCount(2);
        records.setUnansweredCount(1);
        List<QuestionAnswerContext> questions = new ArrayList<>();
        QuestionAnswerContext context1 = new QuestionAnswerContext();
        Question q1 = new Question();
        q1.setId("100");
        q1.setScore(10);
        q1.setType("single");
        q1.setAnalysis("答案解析解析解析111");
        List<Option> options = new ArrayList<>();
        Option op1 = new Option();
        op1.setId("201");
        op1.setSequence("1");
        op1.setIs_correct("false");
        op1.setText("多继承");
        Option op2 = new Option();
        op2.setId("202");
        op2.setSequence("2");
        op2.setIs_correct("true");
        op2.setText("单继承");
        options.add(op1);
        options.add(op2);
        q1.setOptions(options);
        UserAnswer a1 = new UserAnswer();
        JSONArray answer = new JSONArray();
        answer.add("202");
        a1.setAnswer(answer.toJSONString());
        a1.setScore(10);
        a1.setIs_correct(true);
        a1.setId("1");
        context1.setQuestion(q1);
        context1.setUser_answer(a1);
        QuestionAnswerContext context2 = new QuestionAnswerContext();
        Question q2 = new Question();
        q2.setId("101");
        q2.setScore(5);
        q2.setType("judgment");
        q2.setAnalysis("11fenxi");
        q2.setCorrect_answer("false");
        UserAnswer a2 = new UserAnswer();
        a2.setAnswer("true");
        a2.setScore(0);
        a2.setIs_correct(false);
        a2.setId("2");
        context2.setQuestion(q2);
        context2.setUser_answer(a2);
        questions.add(context1);
        questions.add(context2);
        records.setQuestions(questions);
        System.out.println(JSONObject.toJSONString(records));
        return CommonResult.success(records);
    }

    @Data
    public static class ExamRecords {
        private String examId;//试卷ID
        private String submitTime;//提交时间
        private Integer timeUsed;//考试用时
        private Integer correctCount;//答题-正确数量
        private Integer wrongCount;//答题-错误数量
        private Integer unansweredCount;//答题-未答题数量
        private List<QuestionAnswerContext> questions;//答题详情

    }

    @Data
    public static class QuestionAnswerContext{
        private Question question;
        private UserAnswer user_answer;
    }

    @Data
    private static class Question {
        private String id;
        private Integer score;
        private String type;
        private String analysis;
        private String correct_answer;
        private List<Option> options;
    }

    @Data
    private static class Option {
        private String id;
        private String sequence;
        private String text;
        private String is_correct;
    }
//    @Data
    private static class UserAnswer {
        private String id;
        private Integer score;
        private boolean is_correct;
        private String answer;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public Integer getScore() {
            return score;
        }

        public void setScore(Integer score) {
            this.score = score;
        }

        public boolean isIs_correct() {
            return is_correct;
        }

        public void setIs_correct(boolean is_correct) {
            this.is_correct = is_correct;
        }

        public String getAnswer() {
            return answer;
        }

        public void setAnswer(String answer) {
            this.answer = answer;
        }
    }


}
